package com.zyp.jobseeker.typehandler;

import com.zyp.jobseeker.enums.ApplicationStatus;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * MyBatis自定义类型处理器，用于处理ApplicationStatus枚举类型
 */
public class ApplicationStatusTypeHandler extends BaseTypeHandler<ApplicationStatus> {

    /**
     * 将枚举值转换为数据库存储值
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, ApplicationStatus status, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, status.getCode());
    }

    /**
     * 从结果集字符串列读取并转换为枚举
     */
    @Override
    public ApplicationStatus getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return ApplicationStatus.getByCode(code);
    }

    /**
     * 从结果集索引列读取并转换为枚举
     */
    @Override
    public ApplicationStatus getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int code = rs.getInt(columnIndex);
        return ApplicationStatus.getByCode(code);
    }

    /**
     * 从调用语句中读取并转换为枚举
     */
    @Override
    public ApplicationStatus getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int code = cs.getInt(columnIndex);
        return ApplicationStatus.getByCode(code);
    }
}